import Vue from 'vue'
import VueRouter from 'vue-router'
import home from '../components/home.vue'
import login from '../components/login.vue'
import firsthop from '../views/firsthop.vue'
import teacherList from '../views/teacherList.vue'
import studentList from '../views/studentList.vue'
import allGrade from '../views/allGrade.vue'
import statistics from '../views/statistics.vue'
import myGrade from '../views/forStu/myGrade.vue'
import myStuGrade from '../views/forTea/myStuGrade.vue'
import pic1 from '../components/pic1.vue'
import pic2 from '../components/pic2.vue'
import classStu from '../views/forAdmin/classStu.vue'
import classGrade from '../views/forAdmin/examGrade.vue'

Vue.use(VueRouter)

  const routes = [
    {path:'/',redirect:'/login'},
    {path:'/login', component:login},
    {
      path:'/home',
      component:home,
      redirect:'/firsthop',
      children:[
          {path:'/firsthop', component:firsthop},
          {path:'/teacherList', component:teacherList},
          {path:'/studentList', component:studentList},
          {path:'/allGrade', component:allGrade},
          {path:'/statistics', component:statistics},
          {path:'/myGrade', component:myGrade},
          {path:'/myStuGrade', component:myStuGrade},
          {path:'/pic1', component:pic1},
          {path:'/pic2', component:pic2},
          {path:'/classStu', component:classStu},
          {path:'/classGrade', component:classGrade},
          ]
    },
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
//有了这个没有登录在页面路径输入home，不会发生跳转
router.beforeEach((to,from, next) => {
  if (to.path === '/login') return next()// 如果要访问的是一个有权限的页面
  //获取token
  const tokenStr = window.sessionStorage.getItem('flag')//拿到这个token
  if (!tokenStr) return next('/login') //token字符不存在，就说明没有登录
  else next()
})
//to表示访问的路径
//from表示从哪个路径跳转而来
//next是一个函数，表示放行
//next() 放行 next('/login') 强制跳转

export default router
